(use ./common)


(def [RND_A RND_B] [134775813 1103515245])
(def SEED 1)
(def MASK (dec (math/exp2 32)))
(def nu-arr array/new-filled)
(defn interpolate (a b c d x)
  (def p (- (- d c) (- a b)))
     (+ (* x (+ (* x (+ (* x p) (- a b p))) (- c a))) b))
(defn tile (coordinate period)
  (var coordinate coordinate)
  (if (< coordinate 0) (while (< coordinate 0) (+= coordinate period))
      (% coordinate period)))
#(defn randomize (seed x y)
#  (def [RND-A RND-B] [134775813 1103515245])
#
#  (return #(brushift
#           (* (bxor (* (bxor x y) RND-A) (+ seed x))
#              (bxor (blshift (* RND-B x) 16) (- (* RND-B y) RND-A)))
#           ))
#           #0)))
(def randomize |(:uniform (math/rng (hash $&))))
(defn cubic-config (seed periodX periodY)
  (def cfg {:seed (math/floor (* seed 9007199254740991 )) :periodX periodX :periodY periodY}))

(defn cubic-noise-sample1 ({:seed seed :periodX periodX :periodY periodY}
                           x y)
  (def [xi yi] (map math/floor [x y]))
  (def [lerpX lerpY] (map - [x y] [xi yi]))
  (def [x0 x1 x2 x3] [(map tile
                           (range (dec xi) (+ xi 3))
                           (nu-arr 4 periodX))])
  (def xSamples (nu-arr 4))
  (for i 0 4
    (def y (tile (+ yi i -1) periodY))
    (set (xSamples i) (interpolate
                       ;(map randomize (nu-arr 4 seed)
                             [x0 x1 x2 x3]
                             (nu-arr 4 y))
                             lerpX)))
  (return (+ 0.25 (* 0.5 (interpolate ;xSamples lerpY)))))
